package com.toommi.refuse.service.impl;

import com.toommi.refuse.common.enums.StatisticsTypeEnum;
import com.toommi.refuse.common.util.DateUtil;
import com.toommi.refuse.entity.AppUserPayStatistics;
import com.toommi.refuse.entity.AppUserPayStatisticsExample;
import com.toommi.refuse.mapper.AppUserPayStatisticsMapper;
import com.toommi.refuse.model.AppUserPayStatisticsModel;
import com.toommi.refuse.service.AppUserPayStatisticsService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yangg
 * @since 2019-08-02 15:38
 */
@Service
@AllArgsConstructor
public class AppUserPayStatisticsServiceImpl implements AppUserPayStatisticsService {

    private AppUserPayStatisticsMapper statisticsMapper;

    @Override
    public void addStatistics(long appUserId, BigDecimal money, String refuseType) {

        insertStatistics(appUserId, money, refuseType, StatisticsTypeEnum.DAY, DateUtil.DATE_FORMAT_YMD);
        insertStatistics(appUserId, money, refuseType, StatisticsTypeEnum.MONTH, DateUtil.DATE_FORMATE_YM);

    }

    @Override
    public AppUserPayStatisticsModel getStatistics(long appUserId, String date, String refuseType) {

        AppUserPayStatisticsModel model = new AppUserPayStatisticsModel();
        if (StringUtils.isEmpty(date)) {
            date = DateUtil.formatTimestampToString(System.currentTimeMillis(), DateUtil.DATE_FORMATE_YM);
        }

        AppUserPayStatisticsExample example = new AppUserPayStatisticsExample();
        AppUserPayStatisticsExample.Criteria criteria = example.createCriteria();
        criteria.andAppUserIdEqualTo(appUserId).andDateEqualTo(date);
        if (StringUtils.isEmpty(refuseType)) {
            //不分类
            List<AppUserPayStatistics> statisticsList = statisticsMapper.selectByExample(example);
            for (AppUserPayStatistics statistics : statisticsList) {
                model.setMonthMoney(model.getMonthMoney() + statistics.getMoney().doubleValue());
            }
        } else {
            criteria.andRefuseTypeEqualTo(refuseType);
            //具体的回收分类
            AppUserPayStatistics statistics = statisticsMapper.selectOneByExample(example);
            if (statistics == null) {
                model.setMonthMoney(0.0);
            } else {
                model.setMonthMoney(statistics.getMoney().doubleValue());
            }
        }

        //获取当天的

        AppUserPayStatisticsExample example2 = new AppUserPayStatisticsExample();
        AppUserPayStatisticsExample.Criteria criteria2 = example2.createCriteria();
        date = DateUtil.formatTimestampToString(System.currentTimeMillis(), DateUtil.DATE_FORMAT_YMD);
        criteria2.andAppUserIdEqualTo(appUserId).andDateEqualTo(date);
        if (StringUtils.isEmpty(refuseType)) {
            //不分类
            List<AppUserPayStatistics> statisticsList = statisticsMapper.selectByExample(example2);
            for (AppUserPayStatistics statistics : statisticsList) {
                model.setTodayPayMoney(model.getTodayPayMoney() + statistics.getMoney().doubleValue());
            }
        } else {
            criteria2.andRefuseTypeEqualTo(refuseType);
            //具体的回收分类
            AppUserPayStatistics statistics = statisticsMapper.selectOneByExample(example2);
            if (statistics == null) {
                model.setTodayPayMoney(0.0);
            } else {
                model.setTodayPayMoney(statistics.getMoney().doubleValue());
            }
        }
        model.setAppUserId(appUserId);
        return model;
    }

    private void insertStatistics(long appUserId, BigDecimal money, String refuseType,
                                  StatisticsTypeEnum typeEnum, String format) {
        String nowStr = DateUtil.formatTimestampToString(System.currentTimeMillis(), format);

        AppUserPayStatisticsExample example = new AppUserPayStatisticsExample();
        example.createCriteria().andAppUserIdEqualTo(appUserId)
                .andDateEqualTo(nowStr)
                .andRefuseTypeEqualTo(refuseType)
                .andTypeEqualTo(typeEnum.getCode());

        AppUserPayStatistics statistics = statisticsMapper.selectOneByExample(example);
        if (statistics == null) {
            statistics = new AppUserPayStatistics();
            statistics.setAppUserId(appUserId);
            statistics.setDate(nowStr);
            statistics.setRefuseType(refuseType);
            statistics.setType(typeEnum.getCode());
            statistics.setMoney(money == null ? new BigDecimal(0.0) : money);

            statisticsMapper.insert(statistics);
        } else {

            statistics.setMoney(statistics.getMoney().add(money == null ? new BigDecimal(0.0) : money));
            statisticsMapper.updateByPrimaryKey(statistics);
        }

    }
}
